package com.hy.service.impl;

import com.hy.bean.OrdersPartstable;
import com.hy.bean.RespBean;
import com.hy.dao.OrdersPartstableDao;
import com.hy.dao.OrderstableDao;
import com.hy.dao.impl.OrdersPartstableDaoImpl;
import com.hy.dao.impl.OrderstableDaoImpl;
import com.hy.bean.RespBean;
import com.hy.dao.OrdersPartstableDao;
import com.hy.dao.OrderstableDao;
import com.hy.dao.impl.OrdersPartstableDaoImpl;
import com.hy.dao.impl.OrderstableDaoImpl;
import com.hy.bean.RespBean;
import com.hy.dao.OrderstableDao;
import com.hy.dao.impl.OrderstableDaoImpl;
import com.hy.dto.OrdersDto;
import com.hy.dto.OrdersPartsDto;
import com.hy.dto.OrdersPartstableDto;
import com.hy.service.OrderstableService;
import com.hy.utils.DBManager;
import com.hy.utils.DateUtils;
import org.apache.commons.dbutils.DbUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import java.util.HashMap;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import java.util.List;
import java.util.Map;

@Service
public class OrderstableServiceImpl implements OrderstableService {
    private OrderstableDao orderstableDao = new OrderstableDaoImpl();
    private OrdersPartstableDao ordersPartstableDao = new OrdersPartstableDaoImpl();
    @Override
    public RespBean queryAll(String status,Integer page,Integer limit) {
        List<Map<String,Object>> mapList = orderstableDao.queryAll(status,page,limit);
        long count = orderstableDao.count(status);

        HashMap<String,Object> map = new HashMap<>();
        map.put("data",mapList);
        map.put("total",count);

        return RespBean.success("查询成功",map);
    }

    @Override
    public RespBean queryAllStatus(String status,Integer page,Integer limit) {
        List<Map<String,Object>> mapList = orderstableDao.queryAllStatus(status,page,limit);
        long count = orderstableDao.countStatus(status);

        HashMap<String,Object> map = new HashMap<>();
        map.put("data",mapList);
        map.put("total",count);

        return RespBean.success("查询成功",map);
    }
    @Override
    public RespBean queryAll2(String okstatus,Integer page,Integer limit) {
        List<Map<String,Object>> mapList = orderstableDao.queryAll2(okstatus,page,limit);
        long count = orderstableDao.count2(okstatus);

        HashMap<String,Object> map = new HashMap<>();
        map.put("data",mapList);
        map.put("total",count);

        return RespBean.success("查询成功",map);
    }
    @Override
    public RespBean queryById(Integer id) {
        List<Map<String,Object>> mapList = orderstableDao.queryById(id);
        List<Map<String,Object>> partsMapList = ordersPartstableDao.queryById(id);

        HashMap<String,Object> map = new HashMap<>();
        map.put("data",mapList);
        map.put("partsMapList",partsMapList);

        return RespBean.success("查询成功",map);
    }

    @Override
    public RespBean add(OrdersDto ordersDto) {
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            connection.setAutoCommit(false);

            int id = orderstableDao.insert(ordersDto,connection);
            int ii = 0;
            for (int i = 0; i < ordersDto.getParticularsData().length; i++) {
                OrdersPartstable params =  ordersDto.getParticularsData()[i];
                ii = ordersPartstableDao.insert(params,id,connection);
            }

            if (id > 0 && ii > 0) {
                connection.commit();
                return RespBean.success("添加成功");
            }
            return RespBean.error("添加失败");
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public RespBean update(OrdersPartsDto ordersDto) {
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            connection.setAutoCommit(false);

            int id = orderstableDao.update(ordersDto,connection);
            boolean flag = false;
            Set<Boolean> booleanSet = new HashSet<>();
            for (int i = 0; i < ordersDto.getParticularsData().length; i++) {
                OrdersPartstableDto params =  ordersDto.getParticularsData()[i];
                List<OrdersPartstableDto> list = ordersPartstableDao.queryBy(params.getId());
                if (list == null || list.size() == 0){
                    flag = ordersPartstableDao.insert02(params,connection);
                    booleanSet.add(flag);
                }else {
                    flag = ordersPartstableDao.update(params,connection);
                    booleanSet.add(flag);
                }
            }

            if (id > 0 && !booleanSet.contains("false")){
                connection.commit();
                return RespBean.success("成功");
            }


            return RespBean.error("添加失败");
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public RespBean audit(Integer id, Integer lastUpdatePerson, String status) {

        boolean flag = orderstableDao.audit(id,lastUpdatePerson,status);

        if (flag)
            return RespBean.success("成功");

        return RespBean.error("失败");
    }

    @Override
    public List<Map<String, Object>> select(String orderId, String beginDate, String endDate, String creator) {
        return orderstableDao.select(orderId,beginDate,endDate,creator);
    }

    @Override
    public List<Map<String, Object>> select() {
        return orderstableDao.select();
    }

    @Override
    public RespBean listOrders() {
        return RespBean.success(null,orderstableDao.selectAll());
    }

    @Override
    public RespBean listOrdersAndOrdersParts(Integer id) {
        return RespBean.success(null,orderstableDao.selectOrdersParts(id));
    }
}